﻿using System.Linq.Expressions;
using Basic.OAModel.DB;
using Basic.OARemoteModel;
using Basic.OARemoteModel.DataFillIn.Model;
using Basic.OARemoteModel.FillInItem.Model;
using Basic.OARemoteModel.UserFill.Model;
using LinqKit;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.SqlSugar.LinqKit;

namespace Basic.OADAL.Repository
{
    internal static class WhereLinq
    {
        public static Expression<Func<DBEmpFillInItem, DBEmpDataFillIn, bool>> ToWhere ( this UserFillItemQuery query )
        {
            ExpressionStarter<DBEmpFillInItem, DBEmpDataFillIn> where = LinqKitExtend.New<DBEmpFillInItem, DBEmpDataFillIn>(( a, b ) => a.FillInId == b.Id && b.Status == FillInStatus.审核通过 && a.EmpId == query.EmpId);
            if ( query.Status.HasValue )
            {
                where = where.And(( a, b ) => a.Status == query.Status.Value);
            }
            if ( query.Begin.HasValue )
            {
                where = where.And(( a, b ) => b.EndDate >= query.Begin.Value);
            }
            if ( query.End.HasValue )
            {
                where = where.And(( a, b ) => b.BeginDate <= query.End.Value);
            }
            if ( query.FillType.IsNotNull() )
            {
                where = where.And(( a, b ) => b.FillType == query.FillType);
            }
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(( a, b ) => b.Title.Contains(query.QueryKey));
            }
            return where;
        }
        public static Expression<Func<DBEmpFillInItem, bool>> ToWhere ( this FillInItemQuery query )
        {
            ExpressionStarter<DBEmpFillInItem> where = PredicateBuilder.New<DBEmpFillInItem>(a => a.FillInId == query.FillInId);

            if ( query.QueryKey.IsNotNull() )
            {
                if ( query.QueryKey.Validate(WeDonekRpc.Helper.Validate.ValidateFormat.数字字母) )
                {
                    where = where.And(a => a.EmpNo.Contains(query.QueryKey));
                }
                else
                {
                    where = where.And(a => a.EmpName.Contains(query.QueryKey));
                }
            }
            if ( query.UnitId.HasValue )
            {
                where = where.And(a => a.UnitId == query.UnitId.Value);
            }
            if ( query.DeptId.HasValue )
            {
                where = where.And(a => a.DeptId == query.DeptId.Value);
            }
            if ( query.Status.HasValue )
            {
                where = where.And(a => a.Status == query.Status.Value);
            }
            return where;
        }
        public static Expression<Func<DBEmpDataFillIn, bool>> ToWhere ( this FillInQuery query )
        {
            ExpressionStarter<DBEmpDataFillIn> where = PredicateBuilder.New<DBEmpDataFillIn>(a => a.CompanyId == query.CompanyId);
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.Title.Contains(query.QueryKey));
            }
            if ( query.FillType.IsNotNull() )
            {
                where = where.And(a => a.FillType == query.FillType);
            }
            if ( query.IsInProgress.HasValue )
            {
                DateTime now = DateTime.Now;
                if ( query.IsInProgress.Value )
                {
                    where = where.And(a => a.Status == FillInStatus.审核通过 && SqlFunc.Between(now, a.BeginDate, a.EndDate));
                }
                else
                {
                    where = where.And(a => a.Status != FillInStatus.审核通过 && a.EndDate < now);
                }
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            return where;
        }
    }
}
